package com.ebt.m.proposal_v2.voice.view;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.proposal_v2.base.Constant;
import com.ebt.m.proposal_v2.bean.event.EventTapWindowClose;
import com.ebt.m.proposal_v2.record.RecordingWrapperMp3;
import com.ebt.m.proposal_v2.utils.PermissionUtils;
import com.ebt.m.proposal_v2.widget.base.CompatLinearLayout;
import com.ebt.m.proposal_v2.widget.dialog.EBTConfirmDialog;
import com.ebt.m.proposal_v2.widget.dialog.TapTipDialog;
import com.ebt.m.utils.UIHelper;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import butterknife.BindView;

/**
 * 语音录制
 *
 * @author archer.qi
 *         Created on 2017/5/19.
 */
public class RecordingView extends CompatLinearLayout {

    @BindView(R.id.root)
    RelativeLayout root;
    @BindView(R.id.voice_recording_logo)
    ImageView logo;
    @BindView(R.id.voice_recording_content)
    TextView content;

    private FragmentManager fragmentManager;
    private TapTipDialog mCountDownDialog;
    private TapTipDialog mRecordingDialog;
    private EBTConfirmDialog mPermissionSettingDialog;
    private OnRecordCompleteListener onRecordCompleteListener;

    public RecordingView(Context context) {
        super(context);
    }

    public RecordingView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int setContentView() {
        return R.layout.view_voice_recording;
    }

    @Override
    protected void onBindView() {
        setDefaultStatus();
        RecordingWrapperMp3.getRecorder().setRecordingTarget(Constant.outputTapDir, Constant.outputTapFileName);
        RecordingWrapperMp3.getRecorder().setRecordingRule(120, 1, 10);
        RecordingWrapperMp3.getRecorder().setRecordingListener(new RecordingWrapperMp3.OnRecordingListener() {
            @Override
            public void onCountDown(int countdown) {
                if (mRecordingDialog != null) {
                    mRecordingDialog.dismiss();
                }
                showCountDownMessage(countdown);
            }

            @Override
            public void onRecordingTip(String msg) {
                showTipMessage(msg);
            }

            @Override
            public void onRecordingFinish(String dir, String fileName, long duration) {
                if (mRecordingDialog != null) {
                    mRecordingDialog.dismiss();
                }
                if (mCountDownDialog != null) {
                    mCountDownDialog.dismiss();
                }
                setDefaultStatus();
                if (onRecordCompleteListener != null) {
                    onRecordCompleteListener.onRecordComplete(dir + fileName, duration);
                }
            }

            @Override
            public void onRecordingStart() {
                setRecordingStatus();
                showRecordingMessage();
            }

            @Override
            public void onRecordingVolumeUpdate(int volume) {
                if (mRecordingDialog != null) {
                    mRecordingDialog.setRecordingVolume(volume);
                }
            }

            @Override
            public void onRecordingCancel() {
                if (mRecordingDialog != null) {
                    mRecordingDialog.dismiss();
                }
                if (mCountDownDialog != null) {
                    mCountDownDialog.dismiss();
                }
                setDefaultStatus();
            }

            @Override
            public void onPermissionUndefined() {
                showPermissionDialog();
            }
        });
    }

    private void showPermissionDialog() {
        if (fragmentManager != null) {
            mPermissionSettingDialog = new EBTConfirmDialog();
            mPermissionSettingDialog.setMessage("录音权限已被禁用\n是否开启录音权限？");
            mPermissionSettingDialog.setNegativeButton("拒绝", v -> mPermissionSettingDialog.dismiss());
            mPermissionSettingDialog.setPositiveButton("开启", v -> {
                mPermissionSettingDialog.dismiss();
                PermissionUtils.gotoSettings(getViewContext());
                EventBus.getDefault().post(new EventTapWindowClose());
            });
            mPermissionSettingDialog.show(fragmentManager, "Settings");
        }
    }

    private void setDefaultStatus() {
        logo.setVisibility(VISIBLE);
        content.setText("按住添加语音讲解计划书");
    }

    private void setRecordingStatus() {
        logo.setVisibility(GONE);
        content.setText("松开 结束");
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                try {
                    RecordingWrapperMp3.getRecorder().startRecording();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case MotionEvent.ACTION_UP:
                RecordingWrapperMp3.getRecorder().stopRecording();
                break;
            case MotionEvent.ACTION_CANCEL:
                RecordingWrapperMp3.getRecorder().cancelRecording();
                break;
        }
        return true;
    }

    /**
     * 弹窗提示
     *
     * @param msg
     */
    private void showTipMessage(String msg) {
        if (fragmentManager != null) {
            new TapTipDialog()
                    .showTip(msg)
                    .show(fragmentManager, "Tip");
        }
    }

    /**
     * 显示倒计时
     *
     * @param countDown
     */
    private void showCountDownMessage(int countDown) {
        if (mCountDownDialog == null) {
            mCountDownDialog = new TapTipDialog();
            mCountDownDialog.showCountDown(countDown);
            if (fragmentManager != null) {
                mCountDownDialog.show(fragmentManager, "CountDown");
            }
        } else {
            mCountDownDialog.showCountDown(countDown);
        }
    }

    /**
     * 显示正在录音
     */
    private void showRecordingMessage() {
        mRecordingDialog = new TapTipDialog();
        mRecordingDialog.showRecordingVolume();
        if (fragmentManager != null) {
            mRecordingDialog.show(fragmentManager, "Recording");
        } else {
            UIHelper.makeToast(getContext(), "请重新启动录音窗口");
        }
    }

    /* 必须设置*/
    public void setFragmentManager(FragmentManager manager) {
        fragmentManager = manager;
    }

    /* 录音结束监听*/
    public void setOnRecordCompleteListener(OnRecordCompleteListener listener) {
        onRecordCompleteListener = listener;
    }

    public interface OnRecordCompleteListener {
        void onRecordComplete(String fileName, long duration);
    }

}
